var Generator = require('yeoman-generator');

module.exports = class extends Generator {

    constructor(args, opts) {
        super(args, opts);
        
        this.argument('appname', { type: String, required: true });

    }

    _getPath() {
        if(!this.option.pathName) {
            this.option.pathName = this.options.appname;
        }
        return this.option.pathName;
    }

    _setPath(name){
        this.option.pathName = name;
    }

    // 配置应用
    prompting() {
        return this.prompt(
            [{
                type: 'input',
                name: 'name',
                message: 'your project name',
                default: this.options.appname
            }]
        ).then((answers) => {
            if(answers.name !== this.options.appname) {
                this._setPath(answers.name);
            }
            // this.log('app name:', answers.name);
        });
    }

    // 拷贝应用配置文件
    copyPackage() {
        this.fs.copyTpl(
            this.templatePath('package.json'),
            this.destinationPath(this._getPath() + '/package.json'), {
                projectName: this.options.appname
            }
        );
    }

    // 拷贝webpack config
    copyWebpackConfigs() {
        this.fs.copyTpl(
            this.templatePath('config'),
            this.destinationPath(this._getPath() + '/config')
        );
    }

    // 拷贝工作目录
    copySrc() {
        this.fs.copyTpl(
            this.templatePath('src'),
            this.destinationPath(this._getPath() + '/src')
        );
    }

    // 拷贝静态文件
    copyPublic() {
        this.fs.copyTpl(
            this.templatePath('public'),
            this.destinationPath(this._getPath() + '/public')
        );
    }

    // 拷贝README.md文件
    copyReadMe() {
        this.fs.copyTpl(
            this.templatePath('README.md'),
            this.destinationPath(this._getPath() + '/README.md'), {
                projectName: this.options.appname
            }
        );
    }
}
